#include "propertycontainer.h"

namespace CoreBase
{
   PropertyContainer::PropertyContainer()
   {}

   PropertyContainer::~PropertyContainer()
   {}

   void PropertyContainer::addProperty(ActorProperty* newprop)
   {
      insertProperty(newprop, -1);
   }

   void PropertyContainer::insertProperty(ActorProperty* newprop, int index)
   {
      if(newprop == NULL)
      {
         return;
      }
      PropertyMapType::iterator itor = _propertyMap.find(newprop->getName());
      if(itor != _propertyMap.end())
      {
         return;
      }
      else
      {
         _propertyMap.insert(newprop->getName(),newprop);
         if (index >= 0 && index < (int)_properties.size())
         {
            _properties.insert(_properties.begin() + index, newprop);
         }
         else
         {
            _properties.push_back(newprop);
         }
      }
   }

   void PropertyContainer::removeProperty(const QString& nametoremove)
   {
      PropertyMapType::iterator itor = _propertyMap.find(nametoremove);
      if (itor != _propertyMap.end())
      {
         _propertyMap.erase(itor);
         for (size_t i = 0; i < _properties.size(); ++i)
         {
            if (_properties[i]->getName() == nametoremove)
            {
               _properties.erase(_properties.begin() + i);
               break;
            }
         }
      }
      else
      {
         //log
      }
   }

   ActorProperty* PropertyContainer::getProperty(const QString& name)
   {
      PropertyMapType::iterator itor = _propertyMap.find(name);
      if(itor == _propertyMap.end())
         return NULL;
      else
		  return itor.value().get();
   }

   const ActorProperty* PropertyContainer::getProperty(const QString& name) const
   {
      PropertyMapType::const_iterator itor =  _propertyMap.find(name);
      if(itor == _propertyMap.end())
         return NULL;
      else
		  return itor.value().get();
   }

   void PropertyContainer::getPropertyList(QVector<const ActorProperty*> &proplist) const
   {
      proplist.clear();
      proplist.reserve(_properties.size());
      for (size_t i = 0; i < _properties.size(); ++i)
      {
         proplist.push_back(_properties[i].get());
      }
   }

   void PropertyContainer::getPropertyList(QVector<ActorProperty*> &proplist)
   {
      proplist.clear();
      proplist.reserve(_properties.size());
      for (size_t i = 0; i < _properties.size(); ++i)
      {
         proplist.push_back(_properties[i].get());
      }
   }

   void PropertyContainer::copyPropertiesFrom(const PropertyContainer& copyfrom)
   {
      for (size_t i = 0; i < _properties.size(); ++i)
      {
         const ActorProperty* prop = copyfrom.getProperty(_properties[i]->getName());
         if (prop != NULL && !prop->isReadOnly())
         {
            _properties[i]->copyFrom(*prop);
         }
      }
   }
}